# -*- coding: UTF-8 -*-

# 导入threading模块
import threading
import time

# 定义一个线程使用函数
def thread_main(a):
    global count, mutex

    # 获得线程名称
    threadname = threading.currentThread().getName()

    for x in range(0, int(a)):
        # 线程加锁
        mutex.acquire()

        count = count + 1

        # 线程解锁
        mutex.release()

        print(threadname, x, count)

        time.sleep(1)

# 定义主函数
def main(num):
    global count, mutex
    threads = []
    count = 1

    # 创建一个线程锁
    mutex = threading.Lock()

    for x in range(0, num):
        threads.append(threading.Thread(target=thread_main, args=(10,)))

    # 启动所有线程
    for t in threads:
        t.start()

    # 主线程中等待所有子线程退出
    for t in threads:
        t.join()

# 调用主函数
if(__name__ == '__main__'):
    num = 4

    # 创建4个线程
    main(4)